package com.example.controller;


import com.aliyuncs.exceptions.ClientException;
import com.example.pojo.*;
import com.example.service.PatriarchService;
import com.example.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
public class PatriarchController {

    @Autowired
    private PatriarchService patriarchService;
    @Autowired
    private AliOSSUtils aliOSSUtils;

    @GetMapping("/patra/index/{username}")
    public Result getByuserName(@PathVariable String username) {
        log.info("首页展示待办：{}", username);
        Select select = patriarchService.showTask(username);

        return Result.success(select);

    }

    @GetMapping("/patra/{username}")
    public Result getByName(@PathVariable String username) {
        log.info("查询您孩子的课后作业：{}", username);
        List<HomeWork> howkList = patriarchService.list(username);

        return Result.success(howkList);
    }


    @GetMapping("/patra/main/{username}")
    public Result getByMine(@PathVariable String username) {
        log.info("查询您的个人信息：{}", username);
        Patriarch patriar = patriarchService.showMine(username);

        return Result.success(patriar);
    }

    @PutMapping("/patra/zydeta")
    public Result getHomeWorkById(@RequestBody SelectDeta selectDeta) {
        log.info("查询作业的详细信息：{}", selectDeta.getId());
        StudentHomeWork studentHomeWork = patriarchService.showDeta(selectDeta);

        return Result.success(studentHomeWork);
    }


    @PostMapping("/patra/opinion")
    public Result addOpinion(@RequestBody Opinion opinion) {
        patriarchService.addOpinion(opinion);
        return Result.success();
    }


    @GetMapping("/patra/daka/{username}")
    public Result getTask(@PathVariable String username) {
        log.info("查询您孩子的打卡任务：{}", username);
        List<Task> taskList = patriarchService.listTask(username);

        return Result.success(taskList);
    }

    @PutMapping("/patra/dakadeta")
    public Result getTaskById(@RequestBody SelectDeta selectDeta) {
        log.info("查询打卡的详细信息：{}", selectDeta.getId());
        StudentTask studentTask = patriarchService.showTakeDeta(selectDeta);

        return Result.success(studentTask);
    }

    @GetMapping("/patra/progress/{username}")
    public Result listProgress(@PathVariable String username) {
        List<Progress> progressList = patriarchService.listProgress(username);
        return Result.success(progressList);
    }

    @GetMapping("/patra/plan/{username}")
    public Result listPlan(@PathVariable String username) {
        List<Plan> planList = patriarchService.listPlan(username);
        return Result.success(planList);
    }

    @PostMapping("/patra/setpass")
    public Result setPassWord(@RequestBody Pass pass) {
        patriarchService.setPassWord(pass);
        return Result.success();
    }

    @PostMapping("/patra/upload")
    public Result uploadImg(@RequestParam("homeWorkImg") MultipartFile homeWorkImg, @RequestParam String username, @RequestParam Integer id) throws IOException, ClientException {
        String imgUrl = aliOSSUtils.upload(homeWorkImg);
        patriarchService.uplodeHomeWork(username, id, imgUrl);
        return Result.success(imgUrl);
    }

    @GetMapping("/patra/student/{username}")
    public  Result getStudent(@PathVariable String username){
        List<Student> studentList = patriarchService.listStudent(username);
        return Result.success(studentList);

    }



    @GetMapping("/patra/announ/{username}")
    public Result getAnnoun(@PathVariable String username) {
        List<Announcemen> announcemenList = patriarchService.getAnnoun(username);
        return Result.success(announcemenList);

    }

    @GetMapping("/patra/renjiao/{username}")
    public Result getTeacherByClass(@PathVariable String username){
        List<Teacher> teacherList = patriarchService.getTeacherByClass(username);
        return Result.success(teacherList);
    }

    @GetMapping("/patra/money/{username}")
    public Result getMoney(@PathVariable String username) {
        SelectMoney selectMoney = patriarchService.getClassFee(username);
        return Result.success(selectMoney);
    }

    @PostMapping("/patra/uploaddaka")
    public Result uploadTaskImg(@RequestParam("taskImg") MultipartFile taskImg, @RequestParam String username, @RequestParam Integer id) throws IOException, ClientException {
        String imgUrl = aliOSSUtils.upload(taskImg);
        patriarchService.uplodeTask(username, id, imgUrl);
        return Result.success(imgUrl);
    }


    @PostMapping("/admin/setImg")
    public Result setImage(@RequestParam("homeWorkImg") MultipartFile homeWorkImg) throws IOException, ClientException {
        String imgUrl = aliOSSUtils.upload(homeWorkImg);
        return Result.success(imgUrl);
    }
}
